/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Supermarket;

import CustomerGUIs.ListOfCustomerForm;
import Model.Customer;
import Model.InternalDBManager;
import Model.Purchase;
import ProductGUIs.ListOfProductsForm;
import ProductGUIs.ProductsStoresFrame;
import java.io.File;
import StoreGUIs.ListOfStoresForm;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.swing.ImageIcon;

/**
 *
 * @author Manolis
 */


public class MainForm extends javax.swing.JFrame {

    private static LoginUser loginUser;
    private static EntityManager em;

    /**
     * Δημιουργεί τη φόρμα του Admin
     */
    public MainForm(LoginUser loginUser) {
        this.em = InternalDBManager.getEm();
        if (!em.getTransaction().isActive()) {
            em.getTransaction().begin();
        }
        this.loginUser = loginUser;
        this.setIconImage(new ImageIcon(getClass().getResource("..\\Images\\cart.png")).getImage());
        initComponents();
    }

    public MainForm() {
        this.setIconImage(new ImageIcon(getClass().getResource("..\\Images\\cart.png")).getImage());
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        AdminPanel = new javax.swing.JPanel();
        AdminShopsButton = new javax.swing.JButton();
        AdminProductsButton = new javax.swing.JButton();
        AdminRelationsButton = new javax.swing.JButton();
        AdminCustomersButton = new javax.swing.JButton();
        Thes2Button = new javax.swing.JButton();
        XMLReportButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        LoginUserLabel = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Εφαρμογή Διαχείρισης Καταστημάτων Λιανικής");
        setPreferredSize(new java.awt.Dimension(1024, 768));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        AdminPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        AdminShopsButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        AdminShopsButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Shop_50.png"))); // NOI18N
        AdminShopsButton.setText("Καταστήματα");
        AdminShopsButton.setToolTipText("");
        AdminShopsButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        AdminShopsButton.setFocusable(false);
        AdminShopsButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        AdminShopsButton.setIconTextGap(1);
        AdminShopsButton.setPreferredSize(new java.awt.Dimension(70, 30));
        AdminShopsButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        AdminShopsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AdminShopsButtonActionPerformed(evt);
            }
        });

        AdminProductsButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        AdminProductsButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Product_50.png"))); // NOI18N
        AdminProductsButton.setText("Προϊόντα");
        AdminProductsButton.setToolTipText("");
        AdminProductsButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        AdminProductsButton.setFocusable(false);
        AdminProductsButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        AdminProductsButton.setIconTextGap(1);
        AdminProductsButton.setPreferredSize(new java.awt.Dimension(70, 30));
        AdminProductsButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        AdminProductsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AdminProductsButtonActionPerformed(evt);
            }
        });

        AdminRelationsButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        AdminRelationsButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Panel truck_50.png"))); // NOI18N
        AdminRelationsButton.setText("Συσχετίσεις ");
        AdminRelationsButton.setToolTipText("Συσχετίσεις Προϊόντων Καταστημάτων");
        AdminRelationsButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        AdminRelationsButton.setFocusable(false);
        AdminRelationsButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        AdminRelationsButton.setIconTextGap(1);
        AdminRelationsButton.setPreferredSize(new java.awt.Dimension(70, 30));
        AdminRelationsButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        AdminRelationsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AdminRelationsButtonActionPerformed(evt);
            }
        });

        AdminCustomersButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        AdminCustomersButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/customers_50.png"))); // NOI18N
        AdminCustomersButton.setText("Πελάτες");
        AdminCustomersButton.setToolTipText("");
        AdminCustomersButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        AdminCustomersButton.setFocusable(false);
        AdminCustomersButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        AdminCustomersButton.setIconTextGap(1);
        AdminCustomersButton.setPreferredSize(new java.awt.Dimension(70, 30));
        AdminCustomersButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        AdminCustomersButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AdminCustomersButtonActionPerformed(evt);
            }
        });

        Thes2Button.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        Thes2Button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/THES2_75.jpg"))); // NOI18N
        Thes2Button.setToolTipText("");
        Thes2Button.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        Thes2Button.setFocusable(false);
        Thes2Button.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        Thes2Button.setIconTextGap(1);
        Thes2Button.setPreferredSize(new java.awt.Dimension(70, 30));
        Thes2Button.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        Thes2Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Thes2ButtonActionPerformed(evt);
            }
        });

        XMLReportButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        XMLReportButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/XML_50.png"))); // NOI18N
        XMLReportButton.setText("XML Report");
        XMLReportButton.setToolTipText("");
        XMLReportButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        XMLReportButton.setFocusable(false);
        XMLReportButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        XMLReportButton.setIconTextGap(1);
        XMLReportButton.setPreferredSize(new java.awt.Dimension(70, 30));
        XMLReportButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        XMLReportButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                XMLReportButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout AdminPanelLayout = new javax.swing.GroupLayout(AdminPanel);
        AdminPanel.setLayout(AdminPanelLayout);
        AdminPanelLayout.setHorizontalGroup(
            AdminPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(AdminPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(AdminPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(AdminShopsButton, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(AdminProductsButton, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(AdminRelationsButton, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(AdminCustomersButton, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Thes2Button, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(XMLReportButton, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        AdminPanelLayout.setVerticalGroup(
            AdminPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(AdminPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(AdminShopsButton, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(AdminProductsButton, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(AdminRelationsButton, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(AdminCustomersButton, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(XMLReportButton, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 76, Short.MAX_VALUE)
                .addComponent(Thes2Button, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jLabel1.setFont(new java.awt.Font("Arial Black", 1, 36)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Karotsi_515.png"))); // NOI18N
        jLabel1.setText("Super Market V1.0");
        jLabel1.setToolTipText("");
        jLabel1.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jLabel1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        LoginUserLabel.setText(" ");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(LoginUserLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 336, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(1568, Short.MAX_VALUE))
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGap(0, 701, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addGap(0, 701, Short.MAX_VALUE)))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(LoginUserLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGap(0, 89, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addGap(0, 89, Short.MAX_VALUE)))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(AdminPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(2, 2, 2))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(AdminPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(48, 48, 48)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void AdminShopsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AdminShopsButtonActionPerformed
        ListOfStoresForm listOfStoresForm = new ListOfStoresForm();
        listOfStoresForm.setVisible(true);

    }//GEN-LAST:event_AdminShopsButtonActionPerformed

    private void AdminProductsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AdminProductsButtonActionPerformed
        ListOfProductsForm listOfProductsForm = new ListOfProductsForm();
        listOfProductsForm.setVisible(true);
    }//GEN-LAST:event_AdminProductsButtonActionPerformed

    private void AdminRelationsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AdminRelationsButtonActionPerformed
        ProductsStoresFrame productsStoresForm = new ProductsStoresFrame();
        productsStoresForm.setVisible(true);
    }//GEN-LAST:event_AdminRelationsButtonActionPerformed

    private void AdminCustomersButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AdminCustomersButtonActionPerformed
        ListOfCustomerForm listOfCustomerForm = new ListOfCustomerForm();
        listOfCustomerForm.setVisible(true);
    }//GEN-LAST:event_AdminCustomersButtonActionPerformed

    private void Thes2ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Thes2ButtonActionPerformed
        AboutForm aboutForm = new AboutForm(this, true);
        aboutForm.setVisible(true);
    }//GEN-LAST:event_Thes2ButtonActionPerformed

    private void XMLReportButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_XMLReportButtonActionPerformed
        File xmlFile = new File("..\\purchases.xml");
        PurchaseXMLManager purchaseXMLWriter = new PurchaseXMLManager(xmlFile);
        exportPurchases(purchaseXMLWriter);
        AppMessages.XMLFileCreated(this);
    }//GEN-LAST:event_XMLReportButtonActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        LoginUserLabel.setText(loginUser.getUserType() + " - " + loginUser.getUserName()
                + " - " + loginUser.getUserPass());
    }//GEN-LAST:event_formWindowOpened

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainForm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AdminCustomersButton;
    private javax.swing.JPanel AdminPanel;
    private javax.swing.JButton AdminProductsButton;
    private javax.swing.JButton AdminRelationsButton;
    private javax.swing.JButton AdminShopsButton;
    private javax.swing.JLabel LoginUserLabel;
    private javax.swing.JButton Thes2Button;
    private javax.swing.JButton XMLReportButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables

    //ρυθμίζει τον login user 
    public void setLoginUser(LoginUser loginUser) {
        MainForm.loginUser = loginUser;
    }

    private void initAdminButtons() {
        //To change body of generated methods, choose Tools | Templates.
    }

    private void initCustomerButtons() {
        //To change body of generated methods, choose Tools | Templates.
    }

    private Customer findLoginCustomer() {
        Customer customer = new Customer();
        //αν ο χρήστης είναι Πελάτης
        if (loginUser.getUserType().equals("Customer")) {
            EntityManager em = InternalDBManager.getEm();
            if (!em.getTransaction().isActive()) {
                em.getTransaction().begin();
            }
            //βρες τον πελάτη με βάση το userName (κάρτα πόντων)
            customer = (Customer) em.createNamedQuery("Customer.findByPointsCardNumber").
                    setParameter("pointsCardNumber", loginUser.getUserName()).getSingleResult();
        }
        //επέστρεψε πελάτη
        return customer;
    }
    
    /**
     * Εξάγει όλες τις αγορές που είναι αποθηκευμένες στη ΒΔ σε αρχείο XML στη θέση C:\temp
     * @param purchaseXMLWriter 
     */
    private void exportPurchases(PurchaseXMLManager purchaseXMLWriter){
        TypedQuery<Purchase> allPurchasesQuery = em.createNamedQuery("Purchase.findAll", Purchase.class);
        List<Purchase> purchaseResults = allPurchasesQuery.getResultList();                    
        purchaseXMLWriter.writeXML(purchaseResults);
        /*
        System.out.println ( "Το αρχείο XML δημιουργήθηκε με επιτυχία στο C:\\temp" );                    
        try {
            if(!em.getTransaction().isActive())
                em.getTransaction().begin();
            for (Purchase p:purchaseResults){
                em.remove(p);
            }
            em.getTransaction().commit();
            System.out.println("Επιτυχής Διαγραφή συναλλαγών!");            
            System.out.println(" ");
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        }   
                
        */
    }
    

}
